package cn.uce.cc.srv.biz.impl;

import cn.uce.base.PubApiObjectConstants;
import cn.uce.base.entity.CsCustomerInformation;
import cn.uce.base.vo.CsCustomerInformationVo;
import cn.uce.cc.api.service.biz.ICcCustomerInformationBiz;
import cn.uce.cc.srv.client.CcosInterfaceClient;
import cn.uce.cc.srv.utils.HttpClientPubApiUtils;
import cn.uce.common.omg.service.CcCustomerInformationService;
import cn.uce.ics.base.vo.srv.CcRequestVo;
import cn.uce.service.biz.ICsCustomerInformationBiz;
import cn.uce.utils.StringUtil;
import cn.uce.web.common.base.CurrentUser;
import cn.uce.web.common.util.ObjectConvertUtil;
import cn.uce.web.common.util.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ccCustomerInformationBiz")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class CcCustomerInformationBiz implements ICcCustomerInformationBiz {
    private Logger log = LoggerFactory.getLogger(CcCustomerInformationBiz.class);

    @Resource
    ICsCustomerInformationBiz csCustomerInformationBiz;
    @Resource
    CcosInterfaceClient ccosInterfaceClient;

    @Resource
    CcCustomerInformationService ccCustomerInformationService;
    @Override
    public CsCustomerInformationVo getCustomerInformationQueryPhone(String phone) {
//        return ccCustomerInformationService.getCustomerInformationQueryPhone(phone);
        try {
            cn.uce.ics.base.vo.cs.CsCustomerInformationVo vo =  csCustomerInformationBiz.getCustomerInformationQueryPhone(phone);
            if(vo != null){
                return ObjectConvertUtil.convertObject(vo,CsCustomerInformationVo.class);
            }
        }catch (Exception e){
            log.error("查询客户信息异常",e);
        }
        return null;
    }


    @Override
    public int saveByPhone(CsCustomerInformation entity) {
        CurrentUser user = WebUtil.getCurrentUser();
        int rows =-1;
        CsCustomerInformationVo csCustomerInformationVoPhone =new CsCustomerInformationVo();
        csCustomerInformationVoPhone.setPhone(entity.getPhone());
//        List<CsCustomerInformationVo> list =ccCustomerInformationService.getList(csCustomerInformationVoPhone);
//        try {
//            if(list!=null && list.size()>0){
//                entity.setId(list.get(0).getId());
//                rows =upd(entity,user);
//            }else{
//                add(entity,user);
//            }
//        }catch (Exception e){
//            log.error("保存客户信息异常",e);
//        }
        CcRequestVo<cn.uce.ics.base.entity.cs.CsCustomerInformation> vo = new CcRequestVo<cn.uce.ics.base.entity.cs.CsCustomerInformation>();
        vo.setUser(user);
        try {
            cn.uce.ics.base.entity.cs.CsCustomerInformation customerInformation = ObjectConvertUtil.convertObject(entity,cn.uce.ics.base.entity.cs.CsCustomerInformation.class);
            vo.setData(customerInformation);
            csCustomerInformationBiz.saveByPhone(vo);
        }catch (Exception e){
            log.error("调用ICS接口失败");
        }
        return rows;
    }

    /*CsCustomerInfomation
     * 客户信息新增
     * */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int add(CsCustomerInformation entity,CurrentUser currentUser){
//        CurrentUser currentUser = WebUtil.getCurrentUser();
        if(currentUser.getEmpCode().isEmpty() || currentUser.getOrgId()==0){
            return -1;
        }else{
            entity.setCreateEmp(currentUser.getEmpCode());
            entity.setCreateOrg(currentUser.getOrgId());
            return ccCustomerInformationService.add(entity);
        }
    }

    /*
     * 客户信息修改
     * */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int upd(CsCustomerInformation entity,CurrentUser currentUser){
//        CurrentUser currentUser = WebUtil.getCurrentUser();
        if(currentUser.getEmpCode().isEmpty() || currentUser.getOrgId()==0){
            return -1;
        }else{
            entity.setCreateEmp(currentUser.getEmpCode());
            entity.setCreateOrg(currentUser.getOrgId());
            entity.setUpdateTime(new Date());
            return ccCustomerInformationService.upd(entity);
        }
    }
}
